package io.renren.controller;

import cn.hutool.core.util.ObjectUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.PutObjectResult;
import io.renren.cloud.OssFactory;
import io.renren.cloud.StsCloudStorageConfig;
import io.renren.commons.tools.utils.Result;
import io.renren.dto.UploadDTO;
import io.renren.exception.ModuleErrorCode;
import io.renren.service.StsTokenService;
import io.renren.vo.ImgUrlVO;
import io.renren.vo.StsTokenVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 文件上传
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("sts")
@Api(tags="文件上传")
public class StsTokenController {
    @Autowired
    private StsCloudStorageConfig config;
    @Autowired
    private StsTokenService stsTokenService;


    @PostMapping("upload")
    @ApiOperation(value = "上传文件")
    public Result<UploadDTO> upload(@RequestParam("file") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            return new Result<UploadDTO>().error(ModuleErrorCode.UPLOAD_FILE_EMPTY);
        }
        StsTokenVO stsTokenVO=stsTokenService.getStsToken();
        if(ObjectUtil.isNull(stsTokenVO)){
            return new Result<UploadDTO>().error(ModuleErrorCode.OSS_STS_TOKEN_ERROR);
        }
        //上传文件
        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        String url = OssFactory.build(config,stsTokenVO).uploadSuffix(file.getBytes(), extension);

        //文件信息
        UploadDTO dto = new UploadDTO();
        dto.setUrl(url);
        dto.setSize(file.getSize());
        return new Result<UploadDTO>().ok(dto);
    }


    @GetMapping("/download")
    public Result<List<ImgUrlVO>> download(String keys) {
        List<ImgUrlVO> imgUrlVOList=new ArrayList<>();
        if(StringUtils.isNoneBlank(keys)&&(!"null".equals(keys))){
            StsTokenVO stsTokenVO=stsTokenService.getStsToken();
            List<String> keyList=new ArrayList<>();
            List<URL> urlList=new ArrayList<>();
            for(String key:keys.split(",")){
                keyList.add(config.getPrefix()+key.substring(key.lastIndexOf("/")));
            }
            urlList=OssFactory.build(config,stsTokenVO).download(keyList);
            if(!urlList.isEmpty()){
                for(URL url:urlList){
                    int begIndex=url.toString().lastIndexOf("/");
                    imgUrlVOList.add(new ImgUrlVO(url.toString().substring(begIndex+1),url.toString()));
                }
            }
        }
        return new Result<List<ImgUrlVO>>().ok(imgUrlVOList);
    }
}
